package com.test.controller;

import com.test.controller.Admininfo.ResData;
import com.test.entity.*;
import com.test.service.*;
import com.test.uitl.VerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

//    管理员
    @Autowired
    private AdminInfoService adminInfoService;
    @Autowired
    private LeaderInfoService leaderInfoService;
    @Autowired
    private HrInfoService hrInfoService;
    @Autowired
    private CwInfoService cwInfoService;
    @Autowired
    private StaffInfoService staffInfoService;

    @GetMapping("login")
    public String getLogin(){
        return "sys_login";
    }

    @GetMapping("validCode/code")
    public void getCode(HttpServletRequest request, HttpServletResponse response){
        VerifyUtil.getRandcode(request,response);
    }

    @PostMapping("login/sysSubmit")
    @ResponseBody
    public ResData postSysSubmit(String imgCode, String name, String password, Integer loginType, HttpSession session){
        Map<String,Object> map=new HashMap<>();

        ResData resData = new ResData();
        String verifyCode =(String) session.getAttribute("verifyCode");

        if (imgCode==null||imgCode.equals("")){
            resData.setCode(0);
            resData.setMsg("请输入验证码");
            return resData;
        }
        if (!imgCode.equals(verifyCode)){
            resData.setCode(0);
            resData.setMsg("验证码输入错误！请重新输入");
            return resData;
        }
        if (loginType==null){
            resData.setCode(0);
            resData.setMsg("请选择登录角色");
            return resData;
        }
        if (name==null||name.equals("")){
            resData.setCode(0);
            resData.setMsg("请输入登录账号");
            return resData;
        }
        if (password==null||password.equals("")){
            resData.setCode(0);
            resData.setMsg("请输入登录密码");
            return resData;
        }

        if (loginType==1){
            AdminInfo admin = adminInfoService.selectByAdmin(name,password);
            System.out.println(admin);
            if (admin.getId()!=null){
                resData.setCode(1);
                resData.setMsg("登录成功");
                map.put("name",admin.getLoginName());
                session.setAttribute("roleName","管理员");
                session.setAttribute("login",map);
                return resData;
            }else {
                resData.setCode(0);
                resData.setMsg("账号或密码错误请重新输入！");
                return resData;
            }

        }else if (loginType==2){
            LeaderInfo leaderInfo = leaderInfoService.selectByLeader(name,password);
            if (leaderInfo.getId()!=null||!leaderInfo.getId().equals("")){
                resData.setCode(1);
                resData.setMsg("登录成功");
                map.put("name",leaderInfo.getRealName());
                map.put("id",leaderInfo.getId());
                session.setAttribute("roleName","部门领导");
                session.setAttribute("login",map);
                return resData;
            }else {
                resData.setCode(0);
                resData.setMsg("账号或密码错误请重新输入！");
                return resData;
            }

        }else if (loginType==3){
            HrInfo hrInfo = hrInfoService.selectByHrInfo(name,password);

            if (hrInfo.getId()!=null||!hrInfo.getId().equals("")){
                resData.setCode(1);
                resData.setMsg("登录成功");
                map.put("name",hrInfo.getRealName());
                map.put("id",hrInfo.getId());
                session.setAttribute("roleName","人力资源");
                session.setAttribute("login",map);
                return resData;
            }else {
                resData.setCode(0);
                resData.setMsg("账号或密码错误请重新输入！");
                return resData;
            }

        }else if (loginType==4){

            CwInfo cwInfo = cwInfoService.selectByCwInfo(name,password);

            if (cwInfo.getId()!=null||!cwInfo.getId().equals("")){
                resData.setCode(1);
                resData.setMsg("登录成功");
                map.put("name",cwInfo.getRealName());
                map.put("id",cwInfo.getId());
                session.setAttribute("roleName","财务管理");
                session.setAttribute("login",map);
                return resData;
            }else {
                resData.setCode(0);
                resData.setMsg("账号或密码错误请重新输入！");
                return resData;
            }

        }else if (loginType==5){

            StaffInfo staffInfo = staffInfoService.selectByCwInfo(name,password);

            if (staffInfo.getId()!=null||!staffInfo.getId().equals("")){
                resData.setCode(1);
                resData.setMsg("登录成功");
                map.put("name",staffInfo.getRealName());
                map.put("id",staffInfo.getId());
                session.setAttribute("roleName","普通员工");
                session.setAttribute("login",map);
                return resData;
            }else {
                resData.setCode(0);
                resData.setMsg("账号或密码错误请重新输入！");
                return resData;
            }

        }
        resData.setCode(0);
        resData.setMsg("登录失败");
        return resData;

    }

}
